package jasypt;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;

/**
 * @Author zhangtao
 * @create 2023/3/9 11:23
 */
@Slf4j
public class PropertiesEncryptor implements StringEncryptor {
    /**
     * AES 密钥
     */
    private String secretKey;

    /**
     * Instantiates a new Properties encryptor.
     *
     * @param secretKey the secret key
     */
    public PropertiesEncryptor(String secretKey) {
        this.secretKey = secretKey;
    }


    @Override
    public String encrypt(String s) {
        try {
            return AesUtils.aesPKCS7PaddingEncrypt(s, secretKey);
        } catch (Exception ex) {
            log.error("加密失败", ex);
        }
        return s;
    }

    @Override
    public String decrypt(String s) {
        try {
            return AesUtils.aesPKCS7PaddingDecrypt(s, secretKey);
        } catch (Exception ex) {
            log.error("配置文件解密失败", ex);
        }
        return s;
    }

    public static void main(String[] args) {
        PropertiesEncryptor encryptor = new PropertiesEncryptor("Boh.Yum.Cash2010");
        log.info("密钥：{}", encryptor.decrypt("90eddbc9e2d272716cb0e092bed1e149"));
    }
}
